﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRGeometry;
using StructureModelDeduction.Data;

namespace StructureModelDeduction.Kit
{
	public class IntersectionInformGetterForColumn : IntersectionInformGetter
	{
		public IntersectionInformGetterForColumn(Autodesk.Revit.DB.Document doc, ElementDataPack elemData, ExternalCommandData cmdData) : base(doc, elemData, cmdData)
		{
		}

		public override int CheckRelationType()
		{
			List<Curve> lstLinkDocElemCurve = null;
			(base.ElementData as ColumnDataPack).GetZFaceLoop(out lstLinkDocElemCurve);
			if (this.GetElementInterSecByLinkSolid() == 0)
			{
				return 0;
			}
			Transform transform = Transform.CreateTranslation(new XYZ(0.0, 0.0, 1.0));
			transform.Origin = new XYZ(0.0, 0.0, 0.0);
			foreach (ElementId elementId in this.m_lstCurrentId)
			{
				ColumnDataPack currentDocElemData = new ColumnDataPack(this.m_Doc, ElementId.InvalidElementId, elementId, transform, this.m_cmdData);
				this.CheckRalationTypeSingleCouple(lstLinkDocElemCurve, currentDocElemData);
			}
			return 1;
		}

		public override void CheckRalationTypeSingleCouple(List<Curve> lstLinkDocElemCurve, ElementDataPack CurrentDocElemData)
		{
			List<Curve> lstCurves = null;
			(CurrentDocElemData as ColumnDataPack).GetZFaceLoop(out lstCurves);
			List<XYZ> list = new List<XYZ>();
			foreach (Curve curve in lstLinkDocElemCurve)
			{
				list.Add(curve.GetEndPoint(0));
				list.Add(curve.GetEndPoint(1));
				if (curve is Arc)
				{
					list.Add(curve.Evaluate(0.3, true));
					list.Add(curve.Evaluate(0.6, true));
					list.Add(curve.Evaluate(0.9, true));
				}
			}
			int num = 0;
			foreach (XYZ pt in list)
			{
				num = Geometry.PointInPloygon(lstCurves, pt);
				if (num != 0)
				{
					break;
				}
			}
			if (num != 0)
			{
				this.m_lstDisPlay.Add(CurrentDocElemData);
				return;
			}
			this.m_lstDeduction.Add(CurrentDocElemData);
		}

		public override int GetElementInterSecByLinkSolid()
		{
			XYZ xyz = new XYZ(base.ElementData.TransFormedBoundBox.Min.X, base.ElementData.TransFormedBoundBox.Min.Y, base.ElementData.TransFormedBoundBox.Min.Z + 0.01);
			XYZ xyz2 = new XYZ(base.ElementData.TransFormedBoundBox.Max.X, base.ElementData.TransFormedBoundBox.Max.Y, base.ElementData.TransFormedBoundBox.Max.Z - 0.01);
			BoundingBoxIntersectsFilter boundingBoxIntersectsFilter = new BoundingBoxIntersectsFilter(new Outline(xyz, xyz2));
			LogicalAndFilter logicalAndFilter = new LogicalAndFilter(new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns), boundingBoxIntersectsFilter);
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Doc);
			filteredElementCollector.WherePasses(logicalAndFilter).WhereElementIsNotElementType();
			foreach (Element element in filteredElementCollector)
			{
				this.m_lstCurrentId.Add(element.Id);
			}
			logicalAndFilter = new LogicalAndFilter(new ElementCategoryFilter(BuiltInCategory.OST_Columns), boundingBoxIntersectsFilter);
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.m_Doc);
			filteredElementCollector2.WherePasses(logicalAndFilter).WhereElementIsNotElementType();
			foreach (Element element2 in filteredElementCollector2)
			{
				this.m_lstCurrentId.Add(element2.Id);
			}
			return this.m_lstCurrentId.Count;
		}
	}
}
